package me.stone.stanimeclient.core.data.util

import me.stone.stanimeclient.core.model.FilterTagData
import me.stone.stanimeclient.core.model.FilterTagItem
import java.util.Calendar
import java.util.Locale

object AgeFanFilterDataUtils {

    private val fullData: List<FilterTagData> by lazy {
        buildTags()
    }

    val commonData = fullData.filter {
        // 常用标签不包含“首字母”和“资源”
        !(it.typeValue == "letter" || it.typeValue == "resource")
    }

    private fun buildTags() = listOf(
        region(),
        genre(),
        letter(),
        year(),
        season(),
        status(),
        label(),
        resource(),
        order()
    )

    private fun buildFilterTagData(
        typeName: String,
        typeValue: String,
        tagNames: Map<String, String>,
        tagValues: Array<String>
    ) = FilterTagData(
        typeName = typeName,
        typeValue = typeValue,
        itemList = tagValues.map { value ->
            FilterTagItem(
                typeName = typeName,
                typeValue = typeValue,
                tagValue = value,
                tagName = tagNames[value] ?: value
            )
        }
    )

    private fun region() = buildFilterTagData(
        "地区", "region",
        mapOf("all" to "全部地区"),
        arrayOf("all", "日本", "中国", "欧美")
    )

    private fun genre() = buildFilterTagData(
        "版本", "genre",
        mapOf("all" to "全部版本"),
        arrayOf("all", "TV", "剧场版", "OVA")
    )

    private fun season() = buildFilterTagData(
        "季度", "season",
        mapOf(
            "all" to "全部季度",
            "1" to "1月",
            "4" to "4月",
            "7" to "7月",
            "10" to "10月"
        ),
        arrayOf("all", "1", "4", "7", "10")
    )

    private fun status() = buildFilterTagData(
        "状态", "status",
        mapOf("all" to "全部状态"),
        arrayOf("all", "连载", "完结", "未播放")
    )

    private fun label() = buildFilterTagData(
        "类型", "label",
        mapOf("all" to "全部类型"),
        arrayOf(
            "all", "搞笑", "运动", "励志", "热血", "战斗", "竞技", "校园", "青春",
            "爱情", "恋爱", "冒险", "后宫", "百合", "治愈", "萝莉", "魔法", "悬疑",
            "推理", "奇幻", "科幻", "游戏", "神魔", "恐怖", "血腥", "机战", "战争",
            "犯罪", "历史", "社会", "职场", "剧情", "伪娘", "耽美", "童年", "教育",
            "亲子", "真人", "歌舞", "肉番",
            "美少女", "轻小说", "吸血鬼", "女性向", "泡面番", "欢乐向"
        )
    )

    private fun order() = buildFilterTagData(
        "排序", "order",
        mapOf(
            "time" to "更新时间",
            "name" to "名称"
        ),
        arrayOf("time", "name", "点击量")
    )

    private fun resource() = buildFilterTagData(
        "资源", "resource",
        mapOf("all" to "全部资源"),
        arrayOf("all", "BDRIP", "AGERIP")
    )

    private fun letter(): FilterTagData {
        val values = mutableListOf("all")
        values.addAll(('A'..'Z').map { "$it" })
        return buildFilterTagData(
            "首字母", "letter",
            mapOf("all" to "全部"),
            values.toTypedArray()
        )
    }

    private fun year(): FilterTagData {
        val nowYear = Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR)

        val values = mutableListOf("all")
        values.addAll((nowYear downTo 2000).map { year -> "${year}年" })

        val names = mapOf(
            "all" to "全部年份",
            "2000年" to "2000年以前"
        )

        return buildFilterTagData(
            "年份", "year", names, values.toTypedArray()
        )
    }

}